package com.example.it.studyrecycleviewtwo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;
import java.util.Random;


public class HomeAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private final Random _random = new Random();
    private final List<String> _dataSource;
    private Context _context;

    public HomeAdapter(Context context, List<String> datasource) {
        this._context = context;
        this._dataSource = datasource;
    }

    /*
    * 返回ViewHolder
    * */
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this._context).inflate(R.layout.recycleview_item, parent, false);

        MyViewHolder mvh = new MyViewHolder(view);
        return mvh;
    }

    /*
    * 设置视图显示的内容
    * */
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        /*
        * 更改 item的高度
        * */
        holder.tv.setText(this._dataSource.get(position));
        ViewGroup.LayoutParams lp = holder.tv.getLayoutParams();
        lp.height=(_random.nextInt(150) + 200);
    }

    /*
    * 返回要加载项目的数量
    * */
    @Override
    public int getItemCount() {
        return _dataSource.size();
    }
}
